package com.ucode.springboot.starter.db.interceptor;

import java.sql.Connection;
import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

/**
 * 动态设置参数插件
 * 在这里可以完成数据权限、多租户等参数传递，不需要在每个方法上添加这些参数
 * @author: liliang
 * @date: 2020年1月14日 下午2:14:27
 */
@Intercepts({ 
    @Signature(method = "prepare", type = StatementHandler.class, args = { Connection.class }),
    @Signature(method = "query", type = Executor.class, args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) })
public class ParameterInterceptor implements Interceptor{

    private static Log log = LogFactory.get(ParameterInterceptor.class);
    
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        log.info("进入 PrepareInterceptor 拦截器...");
        
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        //获取数据库类型
        String databaseType = properties.getProperty("databaseType");
        if (databaseType != null) {
            log.info("获取的数据库类型:"+databaseType);
        }        
    }

}
